package com.L.yygh.sms.Service.impl;

import com.L.yygh.sms.Config.SmsProperties;
import com.L.yygh.sms.Service.SmsService;
import com.L.yygh.vo.msm.MsmVo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsProperties smsProperties;

    @Override
    public boolean send(String phone, String code){
        try {
            //判断手机号是否为空
            if(StringUtils.isEmpty(phone)) {
                return false;
            }else{
                Credential credential = new Credential(smsProperties.getSecretId(),smsProperties.getSecretKey());

                SmsClient client = new SmsClient(credential, "ap-guangzhou");
                SendSmsRequest request = new SendSmsRequest();

                request.setSmsSdkAppId(smsProperties.getSdkAppId());
                request.setSignName(smsProperties.getSignName());
//                System.out.println(smsProperties.getSignName());
                request.setTemplateId(smsProperties.getTemplateId());
                String[] strings = new String[1];
                strings[0] = phone;
                request.setPhoneNumberSet(strings);
                String[] strings2 = new String[2];
                strings2[0] = code;
                strings2[1] = "2";
                request.setTemplateParamSet(strings2);

                SendSmsResponse response = client.SendSms(request);
                System.out.println(SendSmsResponse.toJsonString(response));
                return true;
            }
        }catch (TencentCloudSDKException e){
            e.printStackTrace();
        }
        return false;
    }

    //MQ发送短信的封装
    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())){
            boolean isSend = this.send(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }

    private boolean send(String phone, Map<String,Object> param){
        try {
            //判断手机号是否为空
            if(StringUtils.isEmpty(phone)) {
                return false;
            }else{
                Credential credential = new Credential(smsProperties.getSecretId(),smsProperties.getSecretKey());

                SmsClient client = new SmsClient(credential, "ap-guangzhou");
                SendSmsRequest request = new SendSmsRequest();

                request.setSmsSdkAppId(smsProperties.getSdkAppId());
                request.setSignName(smsProperties.getSignName());
//                System.out.println(smsProperties.getSignName());
                request.setTemplateId(smsProperties.getTemplateId());
                String[] strings = new String[1];
                strings[0] = phone;
                request.setPhoneNumberSet(strings);
                String[] strings2 = new String[2];
                strings2[0] = "666666";
                strings2[1] = "2";
                request.setTemplateParamSet(strings2);

                SendSmsResponse response = client.SendSms(request);
                System.out.println(SendSmsResponse.toJsonString(response));
                return true;
            }
        }catch (TencentCloudSDKException e){
            e.printStackTrace();
        }
        return false;
    }

}
